<?php
class IndexController extends Ctrl_Base{
    public function indexAction(){
		$tVersion = $this->sqlite()->getRow('select * from db_version limit 1');
		$tDbfile = $this->getupgradedb();
		if(empty($tVersion['version'])){
			$this->sqlite()->query('update from db_version set version = \'' . max($tDbfile) . '\'');

		}else{
			foreach($tDbfile as $tVal){
				if(strnatcasecmp($tVal,$tVersion['version']) > 0){

					$tRes = Tool_Fnc::readfile(WWW_DIR . '/upgrade/data/db/'.$tVal.'.sql');
					$arr = explode("||",$tRes);
					foreach($arr as $v){
						$v = trim($v);
						if(preg_match('/--appdata--/i',$v)){
							$v = str_replace('--appdata--','',$v);
							$this->sqlite('appdata')->query($v);
						}else{
							$this->sqlite()->query($v);
						}
					}
					$this->sqlite()->query('update  db_version set version = \'' . $tVal . '\'');
				}
			}
		}

		
		$tHomepath = exec('echo %APPDATA%');
		$tWalletdatapath = $tHomepath . '/abcoin';
		if(!is_dir($tWalletdatapath)){
			Tool_Fnc::make_dir($tWalletdatapath);	
		}
		if(!is_file($tWalletdatapath.'/abcoin.conf')){
				
			$tStr = "rpcuser=abcoinrpc\r\nrpcpassword=5em948H6pFwNoL6P8u2uggEfTMsZCUL6xwSthPeMkPxc";
			Tool_Fnc::writefile($tWalletdatapath.'/abcoin.conf',$tStr);
		}
		$this->display('Index/index');
    }
	private function getupgradedb(){
			
		$tDbfile = Tool_Fnc::getFile(WWW_DIR . '/upgrade/data/db');
		if(!count($tDbfile)){return ;}
		$tArr = array();
		foreach($tDbfile as $tVal){
			$tArr[] = trim($tVal,'.sql');	
		}
		return $tArr;
	}

	public function logoutAction(){
		$this->display('Index/logout');				
	}
}
